import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../model/space_item.dart';
import '../../spaces/movie_space/record_card.dart';
import '../../spaces/movie_space/record_image_add.dart';

import '../../helper/routeHelper.dart';

typedef MovieDeleteCallback = void Function(SpaceItem r1);
typedef MovieAddCallback = void Function(SpaceItem r1);

class MovieListPage extends StatefulWidget {
  @override
  MovieListPage(this.spaceId, this.ifCanAdd, this.fType);
  final String spaceId;
  final bool ifCanAdd;
  final int fType;

  @override
  MovieListPageState createState() => new MovieListPageState();
}

class MovieListPageState extends State<MovieListPage> {
  List<SpaceItem> RList = <SpaceItem>[];

  bool firstLoading = true;
  ScrollController _scrollController = new ScrollController();
  bool isEnd = false;

  @override
  initState() {
    super.initState();

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getNewer();
  }

  Future<Null> getMore() async {
    // await addOne();
    int count = 5;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await SpaceItem.GetList(widget.spaceId, widget.fType, 1, 5);
    var list2 = list1
        .where((p) => RList.any((x) => x.Guid == p.Guid) == false)
        .toList();

    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list2);
    });
  }

  onDeleted(SpaceItem r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  onAdded(SpaceItem r1) => {
        setState(() {
          RList.insert(0, r1);
        })
      };

  Future<Null> getNewer() async {
    var list1 = await SpaceItem.GetList(widget.spaceId, widget.fType, 1, 5);
    if (firstLoading == true) {
      setState(() {
        firstLoading = false;
      });
    }

    if (list1.length == 0) return;

    var list2 = list1
        .where((p) => RList.any((x) => x.Guid == p.Guid) == false)
        .toList();

    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      RList = list2;
    });
  }

  buildListData(BuildContext context, int index) {
    var x = RList[index];

    return RecordCardHelper.ImgCard(context, x, onDeleted);
    //if (x.RType == 2) return MovieCard.AudioCard(context,x);
    //return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      // appBar: AppBar(title: Text("照片列表"),),
      body: firstLoading == true
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : new RefreshIndicator(
              child: ListView.builder(
                itemCount: RList.length + 1,
                itemBuilder: (context, index) {
                  if (index != RList.length) {
                    return buildListData(context, index);
                  } else {
                    if (isEnd == true) {
                      return Container(
                          margin: EdgeInsets.all(12.0),
                          child: Center(
                              child: Text(
                            "没有更多了",
                            style: TextStyle(color: Colors.grey),
                          )));
                    } else {
                      return new Container();
                    }
                  }
                },
                controller: _scrollController,
              ),
              onRefresh: getNewer,
            ),
      //floatingActionButton:widget.IfCanAdd? MovieCard.MenuButton(context, widget.SpaceId):null,
      //floatingActionButtonLocation: fabMenuLocation,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          RouteHelper.ShowPage(
              context, MovRecordAddPage(widget.spaceId, onAdded, 2));
        },
      ),
    );
  }
}
